package org.example.mall.admin.service.manage.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.example.mall.admin.configuration.exception.BaseException;
import org.example.mall.admin.mapper.manage.SysDictMapper;
import org.example.mall.admin.model.entity.manage.SysDict;
import org.example.mall.admin.model.enums.manage.DictTypeEnum;
import org.example.mall.admin.service.manage.SysDictItemService;
import org.example.mall.admin.service.manage.SysDictService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author hzq
 * @since 2022-01-13
 */
@Service
@AllArgsConstructor
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    private final SysDictItemService sysDictItemService;

    @Override
    public Boolean edit(SysDict dict) {
        SysDict origin = getById(dict.getDictId());
        if (origin.getDictType().equals(DictTypeEnum.SYSTEM.getType())) {
            throw new BaseException("系统字典不允许修改");
        }
        return updateById(dict);
    }

    @Override
    public Boolean del(Integer dictId) {
        SysDict origin = getById(dictId);
        if (origin.getDictType().equals(DictTypeEnum.SYSTEM.getType())) {
            throw new BaseException("系统字典不允许删除");
        }
        sysDictItemService.delByDictId(dictId);
        return removeById(dictId);
    }
}
